<?php 
/**
*	
*/
class fileClassAction extends apiAction
{
	
	/**
	*	判断是否存在
	*/
	public function changeAction()
	{
		$filenum = $this->get('filenum');
		$frs 	 = m('file')->getone("`filenum`='$filenum'");
		$type	 = '0';
		if($frs){
			$filepath = $frs['filepath'];
			$filepathnew	= $frs['filepathnew'];
			if(!isempt($filepath)){
				if(substr($filepath,0,4)=='http'){
					$type='1';
				}else{
					if(file_exists($filepath))$type='1';
				}
			}
			if($type=='0' && !isempt($filepathnew) && file_exists($filepathnew))unlink($filepathnew);//删除缓存
		}
		$gokey	= 'xinhu'.md5($filenum).'';
		c('cache')->set($gokey, $this->agentkey, 20);
		$bda['type']  = $type;
		$bda['gokey'] = $gokey;
		
		//保存用户信息
		$optid 	 = (int)$this->get('optid','0');
		$optname = $this->rock->jm->base64decode($this->get('optname'));
		$face 	 = $this->rock->jm->base64decode($this->get('face'));
		$where 	 = '`agentid`='.$this->agentid.' and `uid`='.$optid.'';
		$uarr['agentid'] = $this->agentid;
		$uarr['platid'] = $this->platid;
		$uarr['uid'] 	 = $optid;
		$uarr['name'] 	 = $optname;
		$uarr['face'] 	 = $face;
		$db = m('usera');
		if($db->rows($where)==0)$where = '';
		$bo = $db->record($uarr, $where);
		if(!$bo)return returnerror($this->rock->error());
		
		return returnsuccess($bda);
	}
	
	/**
	*	推送文档数据过来
	*/
	public function recedataAction()
	{
		$data 		= str_replace(' ','',htmlspecialchars($_POST['data']));
		$fileid 	= (int)$this->post('fileid');
		$optid 		= (int)$this->post('optid');
		$fileext 	= $this->post('fileext');
		$filesize 	= $this->post('filesize');
		$filesizecn	= $this->post('filesizecn');
		$filename	= $this->post('filename');
		$filenum	= $this->post('filenum');
		
		if(substr($data,0,4)!='http'){
			if(isempt($data) || isempt($fileid) || isempt($fileext))return returnerror('数据不完整');
			$filepath = ''.UPDIR.'/'.date('Y-m').'/'.$this->usersrs['xinhuid'].'_'.$fileid.'_'.date('dHis').'_'.rand(100,999).'.'.$fileext.'';
			
			if(!file_exists($filepath))$this->rock->createtxt($filepath, $this->rock->jm->base64decode($data)); //保存文件
			if(!file_exists($filepath))return returnerror('无法创建文件');
			$size = filesize($filepath);
			if($size<1){
				unlink($filepath);
				return returnerror('空文件，创建失败');
			}
		}else{
			$filepath = $data;
		}
		
		$db 	= m('file');
		$where	= "`filenum`='$filenum'";
		if($db->rows($where)==0){
			$where='';
			$uparr['adddt'] 	 	= $this->rock->now;
		}
		$uparr['optid'] 	= $optid;
		$uparr['optname'] 	= $this->post('optname');
		$uparr['fileid'] 	= $fileid;
		$uparr['agentid'] 	= $this->agentid;
		$uparr['platid'] = $this->platid;
		$uparr['fileext'] 	= $fileext;
		$uparr['filesize'] 	= $size;
		$uparr['filenum'] 	= $filenum;
		$uparr['filename'] 	= $filename;
		$uparr['filepath'] 	= $filepath;
		$uparr['filepathnew'] 	= '';

		$bo = $db->record($uparr, $where);
		if(!$bo)return returnerror($this->db->error());
		
		$gokey	= 'xinhu'.md5($filenum).'';
		c('cache')->set($gokey, $this->agentkey, 20);

		return returnsuccess();
	}

	/**
	*	跳转
	*/
	public function gotoAction()
	{
		$filenum = $this->get('filenum');
		$sign 	 = $this->get('sign');
		$stype 	 = (int)$this->get('stype','0'); //0WPS,1onlyoffice
		$otype 	 = (int)$this->get('otype','0'); //0编辑,1预览
		$optid 	 = (int)$this->get('optid','0'); //操作人id
		if(isempt($sign) || isempt($filenum))return '无效访问';
		$msign	 = md5($this->rock->HTTPweb);
		//if($msign != $sign)return '你不能访问';
		
		$frs 	 = m('file')->getone("`filenum`='$filenum'");
		if(!$frs)return '不存在的文件';
		$agentkey = $this->jm->encrypt($this->agentkey);
		$callurl  = $this->get('callurl');
		$optrs 	  = m('usera')->getone('`agentid`='.$this->agentid.' and `uid`='.$optid.'');
		$url	  = '';
		if($stype==0 && getconfig('wpsapp_key')){
			$url 	 = c('wps')->geturl($frs, array(
				'_w_agentkey' => $agentkey,
				'_w_optid'	  => $optid,
				'_w_callurl'  => $callurl,
				'_w_permission' => ($otype==0) ? 'write' : 'read',
			));
		}
		//$url = '';
		if($stype==1 || $url==''){
			if(getconfig('onlyoffice_url'))$url = 'index.php?m=onlyoffice&filenum='.$filenum.'&_w_agentkey='.$agentkey.'&uaid='.$optrs['id'].'&otype='.$otype.'&callurl='.$callurl.'';
		}
		if($url=='')return '没有配置WPS接口信息、也没有配置onlyoffice部署地址';
		
		$this->rock->location($url);
		
		return 'ok';
	}
	
	/**
	*	时时获取文件是否有编辑
	*/
	public function geteditAction()
	{
		
	}

}